package com.kerrykidz.promotion.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.authority.service.IAuUserService;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.dao.model.GblPosDiscountEntity;
import com.kerrykidz.promotion.dao.model.ProCampaignEntity;
import com.kerrykidz.promotion.dao.model.ProGblPrdEntity;
import com.kerrykidz.promotion.service.IPromotionService;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdCateEntity;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingProductInfoService;
import com.kerrykidz.system.util.UserUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.model.TreeModel;

/**
 * 
 * @ClassName: PromotionController 
 * @description: 大致描述该类的用途，内容简短易懂 
 * @author: wolf-yansl 
 * @date: 2014年9月9日 上午11:34:37 
 * @version: V1.0
 *
 */

@RequestMapping("promotion")
@Controller
public class PromotionController {
	@Autowired 
	private IScmInvocingProductInfoService productService;
	
	@Autowired
	private IPromotionService promotionService;
	@Autowired
	private IScmInvocingProductInfoService productInfoService;

	@Autowired
	private IAuUserService userService;
	/**
	 * 返回首页内容
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月9日
	 */
	@RequestMapping("/home")
	public ModelAndView home() throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		model.addObject("pkusrid", UserUtil.getUserFromSession().getUserId());
		
		/**关联门店*/
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	/**
	 * 限时打折查询
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月9日
	 */
	@RequestMapping("/homeWhere")
	@ResponseBody
	public ProCampaignEntity homeWhere(ProCampaignEntity entity) throws RDPException{
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		List<ProCampaignEntity> resultList = promotionService.findLikeCampNameOrTime(entity,null);
		entity.setResults(resultList);
		return entity;
	}
	
	/**
	 * 查询指定促销活动
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月11日
	 */
	@RequestMapping("/seacheByCampId")
	public ModelAndView seacheByCampId(ProCampaignEntity entity) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity_edit");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		List<GblPosDiscountEntity>  discountAll = promotionService.findGblDiscountAll();
		
		/**获取关联商品*/
		List<String> prdIdAll = promotionService.findByProCampPrdIdAll(entity.getPkCampId());
		
		
		model.addObject("discountAll", discountAll);
		model.addObject("currentDate", sdf.format(new Date()));
		model.addObject("entity", entity);
		model.addObject("prdIdAll", prdIdAll);
		
		/**关联门店*/
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());

		model.addObject("loginUser", UserUtil.getUserFromSession());
		
		return model;
	}
	
	
	/**
	 * 创建促销活动 - 页面
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月9日
	 */
	@RequestMapping("/addPromotionHome")
	public ModelAndView addPromotionHome() throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity_new");
		List<GblPosDiscountEntity>  discountAll = promotionService.findGblDiscountAll();
		model.addObject("discountAll", discountAll);
		model.addObject("loginUser", UserUtil.getUserFromSession());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		model.addObject("currentDate", sdf.format(new Date()));
		
		/**关联门店*/
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		
		return model;
	}
	
	/**
	 * 创建促销活动
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月9日
	 */
	@RequestMapping("/addPromotion")
	public ModelAndView addPromotion(ProCampaignEntity entity,@RequestParam("prdlist") List<String> prdlist) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		
		String abbr = UserUtil.getUserFromSession().getAbbrCode();
		String id = PrimaryKeyGenerator.getPrimaryKey(BusinessType.CX, abbr);
		
		entity.setPkCampId(id);
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity.setModiTime(new Date());
		/*entity.setFkBranId(UserUtil.getUserFromSession().getBranId());*/
		
		entity = promotionService.saveProCampaignEntity(entity,prdlist,UserUtil.getUserFromSession());
		model.addObject("entity", entity);
		model.addObject("pkusrid", UserUtil.getUserFromSession().getUserId());
		
		model.addObject("pkusrid", UserUtil.getUserFromSession().getUserId());
		
		/**关联门店*/
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		
		return model;
	}
	/**
	 * 
	 * @param session
	 * @param entity
	 * @param prdlist
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月11日
	 */
	@RequestMapping("/editPromotion")
	public ModelAndView editPromotion(ProCampaignEntity entity,@RequestParam("prdlist") List<String> prdlist) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity.setModiTime(new Date());
		/*entity.setFkBranId(UserUtil.getUserFromSession().getBranId());*/
		
		
		entity = promotionService.updateProCampaignEntity(entity,prdlist,UserUtil.getUserFromSession());
		model.addObject("entity", entity);
		
		/**关联门店*/
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		
		model.addObject("pkusrid", UserUtil.getUserFromSession().getUserId());
		
		return model;
	}
	/**
	 * 删除促销活动
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月11日
	 */
	@RequestMapping("/deleteProCampaign")
	@ResponseBody
	public int deleteProCampaign(ProCampaignEntity entity) throws RDPException{
		promotionService.deleteProCampaignEntity(entity,UserUtil.getUserFromSession());
		return 0 ;
	}
	
	/**
	 * 查询可促销的商品  - 搜素功能
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月10日
	 */
	@RequestMapping("/seacheGblPrd")
	@ResponseBody
	public GblScmPrdEntity/*ProGblPrdEntity*/ seacheGblPrd(ProGblPrdEntity entity) throws RDPException{
		String fdBrandId =  UserUtil.getUserFromSession().getBranId();
		entity.setFkBranId(fdBrandId);
		GblScmPrdEntity proxyEntity = new GblScmPrdEntity();
			
			BeanUtils.copyProperties(entity, proxyEntity);
			proxyEntity.setFkBranId(fdBrandId);
			proxyEntity.setPkPrdId(entity.getPkPrdId());
			proxyEntity.setPrdName(entity.getPrdName());
			proxyEntity.setCateTypes("6,61");
			proxyEntity.setIspaging(true);
			proxyEntity.setIdList(entity.getPrdIdList());
			
			List<GblScmPrdEntity> resultList = productInfoService.getProductInfo(proxyEntity);
			proxyEntity.setResults(resultList);
		
		
		/*List<ProGblPrdEntity> resutlList = promotionService.findLikePrdIdOrPrdName(entity);
		entity.setResults(resutlList);*/
		return proxyEntity;
	}
	/**
	 * 查询促销商品分类  树结构
	 * @param pid
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月10日
	 */
	@RequestMapping("/seachePrdCateTree")
	@ResponseBody
	public List<TreeModel> seachePrdCateTree(String pid) throws RDPException{
		List<TreeModel> treeCate = new ArrayList<TreeModel>();
		promotionService.findPrdCateAll(pid, treeCate);
		return treeCate;
	}
	
	/**
	 * 查询可促销的商品  - 单击商品分类搜素
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月10日
	 */
	@RequestMapping("/seacheCateIdGblPrd")
	@ResponseBody
	public GblScmPrdEntity/*ProGblPrdEntity*/ seacheCateIdGblPrd(ProGblPrdEntity entity,@RequestParam("pid")String pid) throws RDPException{
		String fdBrandId =  UserUtil.getUserFromSession().getBranId();
		entity.setFkBranId(fdBrandId);
		
		GblScmPrdEntity proxyEntity = new GblScmPrdEntity();
		BeanUtils.copyProperties(entity, proxyEntity);
		proxyEntity.setFkBranId(fdBrandId);
		/*proxyEntity.setFkPrdcateId(pid);*/
		
		/**调用何奎递归查询*/
		List<GblScmPrdCateEntity> cateList=productService.recurseCate(pid, true, true);
		proxyEntity.setCateList(cateList);

		proxyEntity.setCateTypes("6,61");
		proxyEntity.setIspaging(true);
		
		List<GblScmPrdEntity> resultList = productInfoService.getProductInfo(proxyEntity);
		proxyEntity.setResults(resultList);
		/*List<ProGblPrdEntity> resutlList = promotionService.findByCatePrdIdOrPrdName(entity, pid);
		entity.setResults(resutlList);*/
		return proxyEntity;
	}

	public static String listToString(List list) {  
	    StringBuilder sb = new StringBuilder();  
	    if (list != null && list.size() > 0) {  
	        for (int i = 0; i < list.size(); i++) {  
	            if (i < list.size() - 1) {  
	                sb.append(list.get(i) + ",");  
	            } else {  
	                sb.append(list.get(i));  
	            }  
	        }  
	    }  
	    return sb.toString();  
	}  

}
